import * as constants from './constants'
import api from '../../../api/api'
import Immutable from 'immutable';


const changeMyCourse = (result) => ({
	type: constants.CHANGE_MYCOURSE,
	value: Immutable.fromJS(result)
})

const changeCourseList = (result) => ({
	type: constants.CHANGE_COURSELIST,
	value: Immutable.fromJS(result)
})

const changeMySubject = (result) => ({
  type: constants.CHANGE_MYSUBJECT,
	value: Immutable.fromJS(result)
})

const changeSubjectCourse = (result) => ({
  type: constants.CHANGE_SUBJECTCOURSE,
	value: Immutable.fromJS(result)
})

export const getMyCourse = (params) => (dispatch) => {
  api.getMySchedule(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeMyCourse(res.data))
      dispatch(getOneDay({
        thisDate: params.startDate
      }))
    }
  })
}

export const getOneDay = (params) => (dispatch) => {
  api.getClassScheduleOneDate(params).then(res => {
    if(res.code === 20000 ) {
      dispatch(changeCourseList(res.data.list))
    }
  })
}

// export const getMySubject = (params) => (dispatch) => {
//   api.getMySubject(params).then(res => {
//     if(res.code === 20000) {
//       dispatch(changeMySubject(res.data))
//     }
//   })
// }

export const getCourseBySubject = (params) => (dispatch) => {
  api.getCourseBySubject(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeSubjectCourse(res.data.result))
      dispatch(changeMySubject(res.data.allSubject))
    }
  })
}